package com.mengfx.service.impl;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mengfx.bean.Image;
import com.mengfx.mapper.ImageMapper;
import com.mengfx.service.ImageService;
import com.mengfx.service.QiNiuService;
import com.mengfx.vo.ImageVo;

@Service("imageService")
public class ImageServiceImpl implements ImageService {

	@Autowired
	private ImageMapper imageMapper;
	
	@Autowired
	private QiNiuService qiNiuService;
	
	public Image loadByImgKey(String imgKey) {
		return imageMapper.loadByImgKey(imgKey);
	}

	public ImageVo imageToImageVo(String imgKey,int size) {
		Image image = this.loadByImgKey(imgKey);
		if(null != image){
			ImageVo imageVo = new ImageVo();
			imageVo.setHeight(image.getHeight());
			imageVo.setWidth(image.getWidth());
			imageVo.setImgkey(image.getImgKey());
			if(size == 0){
				imageVo.setUrl(qiNiuService.genQiniuFileUrl(image.getImgKey()));
			}else
			imageVo.setUrl(qiNiuService.genQiniuFileUrl(image.getImgKey(),size));
			return imageVo;
		}
		return null;
	}

	public boolean deleteImg(String imgKey) {
		return imageMapper.deleteImg(imgKey) > 0;
	}

}
